package se.webbzon.boltzmann.editor.factory.propertyframe;

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.util.Collection;
import java.util.Random;

import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import se.webbzon.boltzmann.editor.mvc.EditorModel;
import se.webbzon.boltzmann.editor.propertyframe.WorldObjectPropertyFrame;
import se.webbzon.boltzmann.editor.propertyframe.WorldStaticObjectPropertyFrame;
import se.webbzon.boltzmann.game.object.pickup.Pickup;
import se.webbzon.boltzmann.game.object.pickup.PickupEnum;
import se.webbzon.oschi01.worldobject.WorldObject;

public class PickupPropertyFrameFactory implements PropertyFrameFactory {

	/*============================================================
	Public Methods
	============================================================*/
	
	@Override public String getWorldObjectName() {
		return Pickup.worldObjectName;
	}

	@Override public WorldObjectPropertyFrame makePropertyFrame(EditorModel model,
																WorldObject instance) {
		
		return new PropertyFrameImpl(model, (Pickup) instance);
	}
	
	/*============================================================
	Private Classes
	============================================================*/
	
	private static final class PropertyFrameImpl extends WorldStaticObjectPropertyFrame {
		
		// The text fields of this property frame
		private final JTextField idInput;
		
		// The combo boxes of this property frame
		private final JComboBox<PickupEnum> typeInput;
		
		/*============================================================
		Constructors
		============================================================*/
		
		public PropertyFrameImpl(EditorModel model, Pickup instance) {
			super(model, instance);
			
			JPanel pickupPanel = new JPanel();
			pickupPanel.setLayout(new GridLayout(2,2));
			
			pickupPanel.add(new JButton(new RandomizeAction(this)));
			idInput = new JTextField("" + instance.getId());
			pickupPanel.add(idInput);
			
			pickupPanel.add(new JLabel("Pickup Type:"));
			typeInput = new JComboBox<PickupEnum>(PickupEnum.values());
			typeInput.setSelectedItem(instance.getType());
			pickupPanel.add(typeInput);
			
			addPanel("Pickup Settings",pickupPanel);	
			initialize();
		}
		
		/*============================================================
		Public Methods
		============================================================*/
		
		@Override public boolean apply(Collection<String> errorMessages) {
			if (super.apply(errorMessages)) try {
				Pickup pickup = (Pickup) getInstance();
				long id = Long.parseLong(idInput.getText());
				pickup.setId(id);
				pickup.setType((PickupEnum) typeInput.getSelectedItem());
				return true;
			} catch (NumberFormatException e) {
				errorMessages.add(e.getMessage());
				return false;
			} else
				return false;
		}
		
	}
	
	/*============================================================
	Private Classes
	============================================================*/
	
	private static final class RandomizeAction extends AbstractAction {
		
		// Auto generated serial version UID
		private static final long serialVersionUID = 6756555335243292131L;
		
		// The property frame associated with this action
		private final PropertyFrameImpl frame;
		
		/*============================================================
		Constructors
		============================================================*/
		
		public RandomizeAction(PropertyFrameImpl frame) {
			this.frame = frame;
			putValue(AbstractAction.NAME, "ID (press to randomize):");
		}
		
		/*============================================================
		Public Methods
		============================================================*/

		@Override public void actionPerformed(ActionEvent arg0) {
			long id = new Random().nextLong();
			frame.idInput.setText("" + id);
		}
		
	}

}
